package co.fitstart.weapp.api.web.controller.coach;

import java.math.BigDecimal;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.entity.result.HttpResult;

import co.fitstart.entity.coach.CoachSigninStatistic;
import co.fitstart.entity.course.schedule.SigninRecord;
import co.fitstart.entity.principal.Principal;
import co.fitstart.weapp.api.service.CoachService;
import co.fitstart.weapp.api.service.CourseHourRecordService;

/**
 * 课时记录
 * @ClassName: CourseHourRecordController  
 * @author cuteHU  
 * @date 2018年11月27日  
 */
@RestController
@RequestMapping("/coach/course-hour-records")
public class CourseHourRecordController {


    private static final Logger logger = LoggerFactory.getLogger(CourseHourRecordController.class);

    @Autowired
    private CoachService coachService;

    @Autowired
    private CourseHourRecordService courseHourRecordService;

    /**
     * @Description:  课时记录 
     * @return HttpResult    返回类型
     */
    @RequestMapping(value="",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult list(@RequestParam("mobile") String mobile) {
        HttpResult httpResult=new HttpResult();
        
        try {
            Principal coach = coachService.load(mobile);
            
            BigDecimal totalHour = courseHourRecordService.getTotalCourseHour(coach);
            BigDecimal todayHour = courseHourRecordService.getTodayCourseHour(coach);
            List<CoachSigninStatistic> records = courseHourRecordService.listCoachSigninStatistic(coach);
            
            httpResult.success().data("totalHour", totalHour)
                                .data("todayHour", todayHour == null ? 0 : todayHour)
                                .data("records", records);
        } catch (Exception e) {
            logger.error("#CourseHourRecordController# list() error", e);
            httpResult.fail("500").message("加载数据失败");
        }
        return httpResult;
    }

    /**
     * @Description:  月课时记录
     * @param  month   参数
     * @return  HttpResult    返回类型
     */
    @RequestMapping(value="/{statisticId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_UTF8_VALUE, params = "mobile")
    public HttpResult detail(@RequestParam("mobile") String mobile,
                             @PathVariable("statisticId") Long statisticId) {
        HttpResult httpResult=new HttpResult();
        
        try {
            Principal coach = coachService.load(mobile);
            
            CoachSigninStatistic statistic = courseHourRecordService.loadCoachSigninStatistic(statisticId);
            List<SigninRecord> records = courseHourRecordService.listCoachSigninRecord(coach, statistic.getMonth());
            
            httpResult.success().data("month", statistic.getMonth().replace("-", "."))
                                .data("monthHour", statistic.getHour().subtract(statistic.getExperienceHour()).subtract(statistic.getGiveHour()))
                                .data("records", records);
        } catch (Exception e) {
            logger.error("#CourseHourRecordController# list() error", e);
            httpResult.fail("500").message("加载数据失败");
        }
        
        return httpResult;
    }
}
